<template>
  <n-card title="系统用户">
    <div>
      <n-button type="primary" dashed size="small" @click="openDialog">
        <template #icon>
          <n-icon>
            <AddCircleOutline />
          </n-icon>
        </template>
        新增
      </n-button>
    </div>

    <br />
    <n-spin :show="loading">
      <n-data-table
        :columns="columns"
        :data="userData"
        default-expand-all
        style="margin-bottom: 10px; width: 100%"
        :max-height="tableHeight"
        :scroll-x="1000"
      />

      <div style="display: flex; justify-content: flex-end">
        <n-pagination
          v-model:page="queryInfo.pageIndex"
          v-model:page-size="queryInfo.pageSize"
          :page-count="total"
          :on-update:page="updatePage"
          :on-update:page-size="updatePageSize"
          show-size-picker
          :page-sizes="[10, 20, 30, 40]"
        />
      </div>
    </n-spin>
    <myDailog
      ref="myDialogRef"
      :showModal="showModal"
      @closeModal="closeModal"
      @getSysUserList="getSysUserList"
    ></myDailog>
  </n-card>
</template>

<script setup lang="ts">
import { getUserList, deleteUserById } from "@/api/platform.ts";
import { h, inject, onMounted, ref } from "vue";
import { userInfoQueryRequest } from "../models/platformModel";
import { NAvatar, NButton, NPopconfirm, NSwitch, NTag } from "naive-ui";
import { AddCircleOutline } from "@vicons/ionicons5";
import myDailog from "./dialog.vue";

const tableHeight = ref(inject("tableHeight"));
const loading = ref(false);
const queryInfo = ref<userInfoQueryRequest>({
  pageIndex: 1,
  pageSize: 10,
});
const columns = ref([
  {
    type: "selection",
    fixed: "left",
  },
  {
    title: "账号",
    key: "account",
  },
  {
    title: "用户名",
    key: "userName",
  },
  {
    title: "头像",
    key: "head",
    render(row) {
      return h(NAvatar, {
        src: row.head,
        round: true,
      });
    },
  },
  {
    title: "角色",
    key: "userRole",
    render(row) {
      if (row.userRole != null) {
        return h(
          NTag,
          {
            style: {
              marginRight: "6px",
            },
            type: "success",
            bordered: false,
          },
          {
            default: () => row.userRole,
          }
        );
      }
    },
  },
  {
    title: "最后登录时间",
    key: "lastLoginTime",
  },
  {
    title: "最后登录IP",
    key: "lastLoginIP",
  },
  {
    title: "是否可用",
    key: "enable",
    render(row) {
      return h(
        NSwitch,
        {
          style: {
            marginRight: "6px",
          },
          onUpdateValue: (value) => {
            window.$message.success(value + "");
          },
          defaultValue: row.enable,
        },
        {
          default: () => row.enable,
        }
      );
    },
  },
  {
    title: "操作",
    key: "actions",
    fixed: "right",
    render(row) {
      return [
        h(
          NPopconfirm,
          {
            positiveButtonProps: {
              size: "tiny",
            },
            negativeButtonProps: {
              size: "tiny",
            },
            onPositiveClick: (e) => {
              deleteUserById({ id: row.id }).then((res) => {
                if (res.code == 200) {
                  window.$message.success(res.message);
                  getSysUserList();
                } else {
                  window.$message.error(res.message);
                }
              });
            },
          },
          {
            trigger: () => {
              return h(
                NButton,
                {
                  style: { marginRight: "10px" },
                  strong: true,
                  secondary: true,
                  type: "error",
                  size: "small",
                },
                { default: () => "删除" }
              );
            },
            default: () => {
              return "确定要删除？";
            },
          }
        ),
        h(
          NButton,
          {
            strong: true,
            secondary: true,
            type: "warning",
            size: "small",
            onClick: () => {
              showModal.value = true;
              myDialogRef.value.init(row.id);
            },
          },
          { default: () => "编辑" }
        ),
      ];
    },
    width: 150,
  },
]);
const userData = ref([]);
const total = ref(0);
const showModal = ref(false);
const myDialogRef = ref<any>(null);

onMounted(() => {
  getSysUserList();
});
const getSysUserList = () => {
  loading.value = true;
  userData.value = [];
  getUserList(queryInfo.value)
    .then((res) => {
      console.log(res);
      userData.value = res.data.result;
      loading.value = false;
      total.value = res.data.totalPage;
    })
    .catch((err) => {
      loading.value = false;
    });
};
const updatePage = (value) => {
  queryInfo.value.pageIndex = value;
  getSysUserList();
};
const updatePageSize = (value) => {
  queryInfo.value.pageSize = value;
  getSysUserList();
};
const openDialog = () => {
  showModal.value = true;
  myDialogRef.value?.init();
};
const closeModal = () => {
  showModal.value = false;
};
</script>
